<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :close-on-click-modal="closeByModal"
    :width="width"
  >
    <component
      v-if="refresh"
      v-bind:is="component"
      :model.sync="componentModel"
      @success="success"
    ></component>
  </el-dialog>
</template>

<script>
export default {
  props: {
    component: {
      default: ""
    },
    title: {
      default: ""
    },
    componentModel: {
      default: ""
    },
    dialogVisible: {
      default: false
    },
    width: {
      default: "50%"
    }
  },
  data() {
    return {
      refresh: true,
      closeByModal: false
    };
  },
  computed: {
    model: {
      get() {
        return this.componentModel;
      },
      set(val) {
        this.$emit("update:componentModel", val);
      }
    },
    visible: {
      get() {
        return this.dialogVisible;
      },
      set(val) {
        this.$emit("update:dialogVisible", val);
      }
    }
  },
  watch: {
    visible: function() {
      this.refresh = false;
      this.$nextTick(() => {
        this.refresh = true;
      });
    }
  },
  methods: {
    success(raw) {
      this.$emit("success", raw);
    },
    fail(raw) {
      this.$emit("fail", raw);
    }
  }
};
</script>

<style scoped></style>
